//
//  STPraticeViewController.swift
//  frameAndGroupAnimationSwift
//
//  Created by Stan on 2017-02-15.
//  Copyright © 2017 stan. All rights reserved.
//

import UIKit

class STPraticeViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        view.backgroundColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //设置遮罩层position
        maskLayer.position = view.center
        
        //设置controller view的遮罩层
        view.layer.mask = maskLayer
        
        //        遮罩层添加动画效果
        view.layer.mask?.add(maskAnimation(), forKey: nil)
        
    }
    
    
    //    MARK: - 懒加载
    lazy var maskLayer: CALayer = {
        let layer = CALayer()
        layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
        layer.contents = UIImage(named: "logo")?.cgImage
        
        return layer
    }()
}


//MARK: - 遮罩动画
extension STPraticeViewController{
    fileprivate func maskAnimation () -> (CAKeyframeAnimation){
        
        //        放大缩小视图，keypath使用bounds
        let maskAni = CAKeyframeAnimation(keyPath: "bounds")
        
        //        动画时间
        maskAni.duration = 1.25
        
        //        动画延迟0.5秒播放
        maskAni.beginTime = CACurrentMediaTime() + 0.5
        
        //        设置关键帧动画的数值
        let starRect = maskLayer.frame
        let tempRect = CGRect(x: 0, y: 0, width: 100, height: 100)
        let finalRect = CGRect(x: 0, y: 0, width: 2000, height: 2000)
        
        maskAni.values = [starRect,tempRect,finalRect]
        
        
        //        设置关键帧动画的运动节奏
        maskAni.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut),CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut),CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
        
        
        //        动画播放结束后是否移除动画
        maskAni.isRemovedOnCompletion = false
        
        //        动画填充模式：kCAFillModeForwards:当动画结束后，layer会一直保持着动画最后的状态
        maskAni.fillMode = kCAFillModeForwards
        
        return maskAni
        
    }
    
    
}











